package com.bysj.money_diary;


import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Kantubiao extends Fragment implements OnClickListener{	
	private RelativeLayout back_rl,next_rl,xian_rl,bing_rl;
	private TextView time_text;
	public int year,month;
	private String nowTime;
	private LineChart linechart;
	private PieChart piechart;
	public int fragmentflag;
	private FragmentManager fragmentmanager;
	private int flag=0;
	private View view;
	

	

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_kantubiao, container, false);
		
		initView();
		fragmentmanager = getFragmentManager();
		fragmentflag = 0;
		SetTabSelection(0);
		return view;
	}   
	/*
     * 初始化视图
     */
    private void initView() {
    	back_rl = (RelativeLayout)view.findViewById(R.id.kantubiao_back_rl);
		back_rl.setOnClickListener(this);
		next_rl = (RelativeLayout)view.findViewById(R.id.kantubiao_next_rl);
		next_rl.setOnClickListener(this);
		xian_rl = (RelativeLayout) view.findViewById(R.id.kantubiao_xian_rl);
		xian_rl.setOnClickListener(this);
		bing_rl = (RelativeLayout) view.findViewById(R.id.kantubiao_bing_rl);
		bing_rl.setOnClickListener(this);
		time_text = (TextView)view.findViewById(R.id.kantubiao_time_text);
		
		year = GetTime.getYear();
		month = GetTime.getMonth();
		if(month<10){
			nowTime = year+"年"+month+"月";
		}else{
			nowTime = year+"年"+month+"月";	
		}
//		time_text.setText(nowTime);
		flag = 0;
		
	}
    
    @Override
	public void onClick(View v) {
    	switch (v.getId()) {
		case R.id.kantubiao_back_rl:
			backMonth(flag);
			break;
		case R.id.kantubiao_next_rl:
			nextMonth(flag);
			break;
		case R.id.kantubiao_xian_rl:
			flag = 0;
			SetTabSelection(0);
			break;
		case R.id.kantubiao_bing_rl:
			flag = 1;
			SetTabSelection(1);
			break;
		}
		
	}

	private void nextMonth(int flag2) {
		month++;
		if(month>12){
			month=1;
			year++;
		}
		if(month<10){
			nowTime = year+"年"+month+"月";
		}else{
			nowTime = year+"年"+month+"月";	
		}
//		time_text.setText(nowTime);
		
	}
	private void backMonth(int flag2) {
		month--;
		if(month<1){
			month=12;
			year--;
		}
		if(month<10){
			nowTime = year+"年"+month+"月";
		}else{
			nowTime = year+"年"+month+"月";	
		}
//		time_text.setText(nowTime);	
		
	}
	
	private void SetTabSelection(int index) {
		clearSeleted();
		fragmentmanager=getFragmentManager();
		FragmentTransaction transaction=fragmentmanager.beginTransaction();
		hideFragment(transaction);
		switch (index){
		case 0:
			xian_rl.setBackgroundResource(R.drawable.jiyibi_bt_bg_s);
			if(linechart == null){
				linechart = new LineChart();
				transaction.add(R.id.kantubiao_container, linechart);
			}else{
				transaction.setCustomAnimations(R.anim.fragement_enter, R.anim.fragment_exit);
				transaction.show(linechart);
			}
			break;
		case 1:
			bing_rl.setBackgroundResource(R.drawable.jiyibi_bt_bg_s);
			if(piechart==null){
				transaction.setCustomAnimations(R.anim.fragement_enter, R.anim.fragment_exit);
				piechart = new PieChart();
				transaction.add(R.id.kantubiao_container, piechart);
			}else{
				transaction.show(piechart);
			}
			break;
		}
		transaction.commit();
		
	}
	private void hideFragment(FragmentTransaction transaction) {
		if(linechart!=null)
			transaction.hide(linechart);
		if(piechart!=null)
			transaction.hide(piechart);
		
	}
	private void clearSeleted() {
		xian_rl.setBackgroundResource(0);
		bing_rl.setBackgroundResource(0);
	}
	
	public int getY(){
		return year;
	}

}
